<?php

class ResultItem {
	
	private $data;
	protected $SecurityObject;
		
	public function __construct($data, $fkSecurityObject="") {
		
		if(is_array($data)) {
			$this->loadData($data, $fkSecurityObject);
		}
		else {
			//trigger_error("No Data given for ResultItem");
		}
	}
	
	public function loadData($data, $fkSecurityObject="") {
		
		if($fkSecurityObject) {
			$this->SecurityObject = new SecurityObject();
				$this->SecurityObject->loadSecurityObject($fkSecurityObject);
		}
		elseif(isset($data["fkSecurityObject"])) {
			
			$this->SecurityObject = new SecurityObject();
			if($data["fkSecurityObject"]=="-1") {
				$this->SecurityObject->addUserPermission(SecurityUser::getUser()->getPkSecurityUser());	
			}
			else {
				$this->SecurityObject->loadSecurityObject($data["fkSecurityObject"]);
			}
			
		}
		
		
		//Load Time Information
		if(isset($data["createDate"])) {
			$data["createDateTS"] = strtotime($data["createDate"]);
			$data["createDateDist"] = Utils::getTimeDistanceText($data["createDateTS"]);
		}

		if(isset($data["changeDate"])) {
			$data["changeDateTS"] = strtotime($data["changeDate"]);
			$data["changeDateDist"] = Utils::getTimeDistanceText($data["changeDateTS"]);
		}

		
		$this->data = $data;
		
	
	}
	
	public function __get($name) {
		
		if($this->canRead() && is_array($this->data)) {
			//TODO: Check if exists;

			return str_replace("\\'", "'", $this->data[$name]);
			
			
			/*
			if(array_search($name, $this->data)) {
				return $this->data[$name];
			}
			else {
				trigger_error("Field '$name' not found!");
			}
			*/
		}
		else {
			//No Acess
			return "[BLOCKED]";
		}
	}
	
	public function canRead()   { return $this->SecurityObject != null && $this->SecurityObject->canRead(); }
	public function canCreate() { return $this->SecurityObject != null && $this->SecurityObject->canCreate(); }
	public function canUpdate() { return $this->SecurityObject != null && $this->SecurityObject->canUpdate(); }
	public function canDelete() { return $this->SecurityObject != null && $this->SecurityObject->canDelete(); }
	public function canAdmin()  { return $this->SecurityObject != null && $this->SecurityObject->canAdmin(); }
	public function getSecurityObject() { return $this->SecurityObject; }
	
	public function getFields() { return array_keys($this->data); }
	public function getValues() { return array_values($this->data); }
	
	
	
	public static function getAllItems($tablename, $sort=null) {
		
		if($sort==null) {
			//Chech if there is a Sortid
			$column = MySQLConnection::getInstance()->quickQuery("SHOW COLUMNS FROM $tablename WHERE Field = 'sortId'");
			
			if(is_array($column)) {
				$addSort = "ORDER BY sortId";
			}
		}
		else {
			$addSort = "ORDER BY $sort";
		}
		
		$sql = "SELECT * FROM $tablename WHERE 1 $addSort";
		return self::getAllByQuery($sql);
	}

	/**
	 * Get Single Row as Item of an Table by a SQL-Query
	 * 
	 * @return ResultItem
	 */
	
	public static function getAllByQuery($sql, $args = null, $classType="ResultItem") {
		
		$conn = MySQLConnection::getInstance();
		$list = array();
		
		$res = $conn->query($sql, $args);
		if($res) {
			$i=0;
			while(($data = @mysql_fetch_array($res))) {
				$i++;
				$data["canMoveUp"] = $i>1;
				$data["canMoveDown"] = $i<mysql_num_rows($res);
				
				array_push($list, new $classType($data));	
			}
		}
		else {
			Logger::logEvent("QueryFailed", __FILE__, __CLASS__, __FUNCTION__, __LINE__, array("query"=>$conn->getLastQuery(), "classType"=>$classType));
		}
		return $list;		
	}
	
	public static function getSingleByQuery($sql, $args=null, $classType="ResultItem") {
		
		$conn = MySQLConnection::getInstance();
		
		$data = $conn->quickQuery($sql, $args);
		
		return new $classType($data);
		
		
	}
}

?>